package com.xtck.ha.fault.modules.controller;

import com.xtck.ha.fault.framework.page.PageResult;
import com.xtck.ha.fault.framework.utils.JsonResult;
import com.xtck.ha.fault.modules.convert.FaultTaskConvert;
import com.xtck.ha.fault.modules.pojo.dto.FaultTaskDTO;
import com.xtck.ha.fault.modules.pojo.entity.FaultTask;
import com.xtck.ha.fault.modules.pojo.vo.FaultTaskVO;
import com.xtck.ha.fault.modules.service.FaultTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
*  controller
*
* @ClassName: FaultTaskController
* @Description: 控制层访问接口
* @author liuyuan
* @date 2025-06-18
*/
@RestController
@RequestMapping("/faultTask1")
@Api(tags={"任务管理"})
public class FaultTaskController  {
    @Autowired
    private FaultTaskService faultTaskService;

    public  FaultTaskService getService() {
        return faultTaskService;
    }
    public FaultTask getEntity() {
        return new FaultTask();
    }
    public FaultTaskDTO getDTO() {
        return new FaultTaskDTO();
    }

    @PostMapping({"/pageList"})
    @ApiOperation(value = "分页")
    public JsonResult<PageResult<FaultTaskVO>> page(@RequestBody FaultTaskDTO query){
        PageResult<FaultTaskVO> page = getService().page(query);

        return JsonResult.ok(page);
    }


    @GetMapping("{id}")
    @ApiOperation(value = "获取信息")
    public JsonResult<FaultTaskVO> get(@PathVariable("id") Long id){
        FaultTask entity = getService().getById(id);

        return JsonResult.ok(FaultTaskConvert.INSTANCE.convert(entity));
    }


    @PostMapping
    @ApiOperation(value = "保存")
    public JsonResult<String> save(@RequestBody FaultTaskVO vo){
        getService().save(vo);
        return JsonResult.ok();
    }

    @PostMapping({"/execTask"})
    @ApiOperation(value = "上线运行")
    public JsonResult<String> exec(@RequestBody FaultTaskVO vo){
        vo.setIsSchedule(true);
        FaultTask faultTask = getService().save(vo);
        return getService().execTask(faultTask);
    }

    /**
     * 开始任务
     */
    @PostMapping("/start")
    @ApiOperation(value = "开始任务")
    public JsonResult<String> startTask(@RequestBody FaultTask faultTask) {
        return getService().execTask(faultTask);
    }

    /**
     * 停止任务
     */
    @PostMapping("/stop")
    @ApiOperation(value = "停止任务")
    public JsonResult<String> stopTask(@RequestBody FaultTask faultTask) {
        return getService().stopTask(faultTask);
    }

    /**
     * 手动试算
     */
    @PostMapping("/computeOnce")
    @ApiOperation(value = "手动试算")
    public JsonResult<String> computeOnce(@RequestBody FaultTaskVO vo) {
        vo.setIsSchedule(false);
        FaultTask faultTask = getService().save(vo);
        return getService().computeOnce(faultTask);
    }

    /**
     * 再次试算
     */
    @PostMapping("/computeAgain")
    @ApiOperation(value = "再次试算")
    public JsonResult<String> computeAgain(@RequestBody FaultTask faultTask) {
        return getService().computeOnce(faultTask);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public JsonResult<String> update(@RequestBody FaultTaskVO vo){
        getService().update(vo);
        return JsonResult.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public JsonResult<String> delete(@RequestBody List<Long> idList){
        getService().delete(idList);

        return JsonResult.ok();
    }

}
